<extend name="layout/main" />

<block name="HEADER">

</block>

<block name="CONTENT">
    <div class="wrapper" >
        <control:grid  id="grid" padding="0" />
        <!--<div class="layui-row">
            <div class="layui-col-xs3" >
                <div class="panel-heading">
                    <h1>菜单列表树</h1>
                </div>
                <div id="tree"></div>
            </div>
            <div class="layui-col-xs9">
                <div class="content">
                    <control:grid  id="grid" padding="0" />
                </div>    
            </div>
        </div>-->
    </div>
    
</block>

<block name="FOOTER">
   <script type="text/javascript">
   	var page=(function(ns){
    
	    var pwdForm,tree;
	    
	    ns.templet =function(e){
	        var level  = (e.data.sort || '').split('-').length;
	        var icon = (e.data.icon || "").replace('&amp;','&');
	        var src = '<i class="iconfont" style="line-height: 0;font-size: 14px;">';
	        src += level == 1 ? (icon || "&#xe6a8;") : (icon || '&#xe61f;');
	        src += '</i> ';
	        src += e.value;
	       return  src;
	    }
	    
	    //页面初始化
	    ns.init=function(){
	        grid.loadSuccess(function(){
	            grid.setHeadHtml('<i class="iconfont icon-zhankai" onclick="page.opne()"></i>');
	        });
	        //ns.initTree();
	    }
	    
	    //展开和收缩
	    ns.is_open = true;
	    ns.opne=function(){
	        ns.is_open = !ns.is_open;
	       $.each(grid.getRows(), function(index,o) {
	            grid.getObj().treeNodeOpen(o,ns.is_open); 
	        });
	    }
	    
	    //初始化树
	    ns.initTree=function(){
	        tree = layui.tree.render({
	            elem: '#tree' ,
	            textField:"nav_name",
	            idField:"nav_id",
	            parentField:'nav_pid',
	            data:'/admin/soft/nav/getTree',
	            isList:true,                         //是否列表结构数据，如果是，idField，parentField 必填
	            expandNode:0,                //可选，默认展开的节点，字符串
	            showCheckbox: false,        //是否显示复选框
	            accordion: true,               //是否开启手风琴模式
	            showIcon:function(e){       //显示图标，可为true
	                e.icon = (e.icon || "").replace('&amp;','&');
	                return e.children[0] || e.nav_pid == 0 ? e.icon || "&#xe6a8;" : e.icon || '&#xe61f;';
	            },
	            treeMenu:[                           //可选，存在时则显示更多按钮
	                {title:"新增",onClick:"page.addNode()"},
	                {title:"编辑",onClick:"page.editNode()"},
	                {title:"删除",onClick:"page.removeNode()",enable:function(e){return e.nav_pid != 0;}}
	            ],
	            loadSuccess:function(data){  //加载成功
	                data[0] && tree.selectNode("org_id",data[0].org_id);
	            },
	            selectedNode: function(obj){ // 选中节点
	                ns.query();
	            },
	        });
	    }
	    
	   //查询
	    ns.query=function(){
	        var filter =  grid.$search.getData();
	        var node = tree.getSelectedNode();
	        var ids = $.map( tree.getAllChildNodes(), function(h) {
	            return h.nav_id;
	        });
	        if(ids){
	            filter.push({opera: "in", field: "nav_id", value: ids.join(',') });
	        }
	        grid.load(filter);
	    }
	    
	    //新增节点
	    ns.addNode=function(){
	        var  node = tree.getThisNode(); 
	        layer.prompt({
	            title:'请输入组织名称',
	            formType: 3,
	            offset: '25%'
	        }, function(text, index){
	            var param={ parent_org_id: node.org_id, org_name:text }
	            $.ajaxs("/admin/soft/system_user/addOrgData",param,function(res){
	                param.org_id= res.data;
	                tree.addNode(param,node);
	                $.toast(1,"成功新增！");
	            });
	            layer.close(index);
	        });
	    }
	    
	    //编辑节点
	    ns.editNode=function(){
	        var  node = tree.getThisNode(); 
	        layer.prompt({
	            title:'重命名',
	            formType: 3,
	            offset: '25%',
	            value:node.org_name
	        }, function(text, index){
	            var param={org_id:node.org_id,org_name:text,parent_org_id:node.parent_org_id}
	            $.ajaxs("/admin/soft/system_user/addOrgData",param,function(res){
	                tree.setNodeText(node,text);
	                $.toast(1,"成功更新！");
	            });
	            layer.close(index);
	        });
	    }
	    
	    //删除节点
	    ns.removeNode=function(){
	        var  node = tree.getThisNode();
	        var ids = $.map( tree.getAllChildNodes(node), function(h) { return h.org_id;}).join(',');
	        $.confirm('确定删除【'+node.org_name+'】?',function(index){
	            $.ajaxs("/admin/soft/system_user/deleteOrgData",{orgIds:ids},function(res){
	                tree.removeNode(node);
	                $.toast(1,"成功删除！");
	            });
	            layer.close(index);
	        });
	    }
	    
	    //备份
	    ns.upload=function(){
	        var rows=grid.getRows();
	        if(!rows[0]){
	           $.toast(0,"数据异常，不能进行备份！");
	           return;
	        };
	        $.confirm("确定覆盖上次的备份？", function(){
	            $.ajaxs("/admin/soft/nav/backups").then(function(res){
	                $.toast(1,"备份成功！");
	            });
	        });
	    }
	    
	    //还原
	    ns.reduction=function(){
	        $.ajaxs("/admin/soft/nav/getLastBackupsDate").then(function(res){
	            $.confirm("上次的备份时间是："+res.data+" </br>确定还原？", function(){
	                $.ajaxs("/admin/soft/nav/reduction").then(function(res){
	                    $.toast(1,"还原成功！");
	                    grid.load();
	                    seedGrid.load();
	                });
	            });
	        });
	    }
	    
	    //导出
	    ns.exports=function(){
	        $.exports("/admin/soft/nav/exportExcel");
	    }
	    
	    //导入
	    ns.imports=function(){
	        $.openImportHandle('/admin/soft/nav/importHandle',function(resp){
	            $.toast(1,"导入成功！");
	            grid.load();
	            seedGrid.load();
	        });
	    }
	   
	    //新增
	    ns.add=function(){
	        var filter={
	            url:"system/navMng/edit",
	            oid:"",
	            mode:1,
	            nav_pid:0
	        }
			$.openIframe(filter, "新增父页面",['600px', '500px']);
	    }
	    
	    //编辑
	    ns.edit=function(){
	        var selectedRow=grid.getSelectedRows()[0];
	        var filter={
	            url:"system/navMng/edit",
	            oid:selectedRow.nav_id,
	            mode:2
	        }
	        $.openIframe(filter, "编辑父页面",['750px', '500px']); 
	    }
	    
	    //删除
	    ns.del=function(){
	    	if(grid.getSelectedRows()[0].children[0]){
	    		return $.toast(0,'存在子导航，不能删除！');
	    	}
	        grid.deleteSelectRow();
	    }
	    
	    //新增子页面
	    ns.addSeed=function(){
	        var selectedRow=grid.getSelectedRows()[0];
	        if(!selectedRow){
	            $.toast(0,"请先选择父页面数据！");
	            return;
	        }
	        var filter={
	            url:"system/navMng/edit",
	            oid:"",
	            mode:1,
	            nav_pid:selectedRow.nav_id,
	            nav_name:selectedRow.nav_name
	        }
	        $.openWin(filter, "新增子页面",['750px', '500px']); 
	    }
	    
	    //启用与禁用
	    //mode=1 等于禁用
	    ns.enableAndStand=function(mode){
	        var grids=grid;
	        if(mode>1){
	            grids = seedGrid;
	            mode =  mode == 2 ? 0 : 1;
	        }
	        var selectedRow=grids.getSelectedRows();
	        if(!selectedRow[0]){
	            $.toast(0,"请先选择数据！");
	            return;
	        }
	        var param = { 
	            data:$.map(selectedRow, function(h) {
	                return {
	                    nav_id: h.nav_id,
	                    nav_status: mode
	                }
	            })
	        }
	        $.ajaxs("/admin/soft/nav/setEnable",param,function(res){
	            grids.load()
	        });
	    }
	    
	    //移动
	    ns.move=function(mode){
	        var grids=grid;
	        if(mode>1){
	            grids = seedGrid;
	            mode =  mode == 2 ? 0 : 1;
	        }
	        var selectedRow=grids.getSelectedRows();
	        if(!selectedRow[0]){
	            $.toast(0,"请先选择数据！");
	            return;
	        }
	        if(selectedRow.length>1){
	            $.toast(0,"只能选择一条数据！");
	            return;
	        }
	        var rows = $.map(grids.getRows(),function(h){
	            if(selectedRow[0]._level == h._level) return h;
	        });
	        var coverId=-1;
	        rows.forEach(function(h,index){
	            if(selectedRow[0].nav_id == h.nav_id){
	                if(mode==0){
	                    if(rows[index-1]){
	                        coverId = rows[index-1].nav_id;
	                    }
	                }else{
	                    if(rows[index+1]){
	                        coverId = rows[index+1].nav_id;
	                    }
	                }
	            }
	        });
	        var param={ moveId:selectedRow[0].nav_id, coverId:coverId}
	        $.ajaxs("/admin/soft/nav/sortMove",param,function(res){
	            grids.load()
	        });
	    }
	    
	    
	    return ns;
	}({}));
	
	$(function(){ page.init();  })
   </script>
</block>



